package com.itheima.health.job;

import com.itheima.health.service.ReportService;
import com.itheima.health.vo.Date_MemberCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ReportCaluJob {
    @Autowired
    private ReportService reportService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * //计算十二个月前的会员总数量 每个月一次
     */
    @Scheduled(cron = "0 10 0 1 * ?") //  每月1号 0点10分触发一次
    public void  memberCountBefore12Month(){
        Integer count = reportService.memberCountBefore12Month();
        redisTemplate.opsForValue().set("total",count);
    }

    /**
     * //计算从本月,往前推12个月 每个月的会员数量 每月一次
     */
    @Scheduled(cron = "0 0 3 * * ?") //  每天凌晨3点触发一次
    public void memberCountByMonth(){
       List<Date_MemberCount> date_memberCounts =  reportService.memberCountByMonth();
        for (Date_MemberCount dmc : date_memberCounts) {
            redisTemplate.boundHashOps("memberReport").put(dmc.getDate(),dmc.getCount());
        }
    }

}
